package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.muqi.app.im.widget.HomeWkDialog;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.AppUtils;
import com.muqi.app.project.utils.ShareDialog;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.project.utils.ToastUtil;
import com.muqi.app.qlearn.adapter.HomeWorkAdapter;
import com.muqi.app.qlearn.adapter.NoticeAdapter;
import com.muqi.app.qlearn.modles.Banner;
import com.muqi.app.qlearn.modles.HomeWorkBean;
import com.muqi.app.qlearn.modles.LastRecord;
import com.muqi.app.qlearn.modles.NoticeBean;
import com.muqi.app.qlearn.student.BaseFragment;
import com.muqi.app.qlearn.student.MenuActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.ui.NoticeDetailsActivity;
import com.muqi.app.qlearn.ui.SystemNoticeActivity;
import com.muqi.app.user.widget.SelectableRoundedImageView;
import com.muqi.app.widget.pulltorefresh.XListView;

@SuppressLint("InflateParams")
public class NotificationFragment extends BaseFragment implements
		XListView.IXListViewListener, OnItemClickListener,
		HomeWkDialog.OnClickMenuListener, NoticeAdapter.OnHomeWorkListener {
	public static final int REQUEST_REFRESH_CODE = 10010;
	private XListView mListview;
	private HomeWkDialog myDialog = null;
	private int page = 1;
	private List<NoticeBean> myNoticeLists = new ArrayList<NoticeBean>();
	private NoticeAdapter mAdapter = null;
	private ShareDialog shareDialog;
	private boolean isNotification;
	private int lastClickposition = -1;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.aty_my_homework, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		mListview = (XListView) view.findViewById(R.id.xListView);
		mListview.setXListViewListener(this);
		mListview.setPullLoadEnable(false);
		mListview.setPullRefreshEnable(true);
		mListview.setOnItemClickListener(this);
		loadingServiceDatas();
	}

	private void loadingServiceDatas() {
		if (NetWorkUtils.isNetworkAvailable(mActivity)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("student_id", mSpUtil.getCurrentChildId());
			sendMap.put("page", page);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.GET_MY_NOTICE_LISTS_API, sendMap);
			MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					onLoadView();
					hideLoading();
					if (resuleCode == RESULTCODE.SUCCESS) {
						List<NoticeBean> getLists = ResponseUtils
								.getMyNoticeLists(mActivity, result);
						if (getLists != null && getLists.size() > 0) {
							if (page == 1) {
								if (getLists.size() == 0) {
									showEmpty("还没有收到任何通知");
									return;
								} else {
									hidEmpty();
								}
								myNoticeLists.clear();
								myNoticeLists = getLists;
								if (mAdapter != null)
									mAdapter = null;
								mAdapter = new NoticeAdapter(mActivity,
										myNoticeLists,
										NotificationFragment.this);
								mListview.setAdapter(mAdapter);
							} else {
								for (NoticeBean item : getLists) {
									myNoticeLists.add(item);
								}
								mAdapter.notifyDataSetChanged();
							}
							if (getLists.size() < 10) {
								mListview.setPullLoadEnable(false);
							} else {
								mListview.setPullLoadEnable(true);
								page++;
							}
						}
					}
				}
			});
		} else {
			hideLoading();
			ShowToast.showShort(mActivity, R.string.net_break);// 提示无网络
		}
	}

	private void onLoadView() {
		mListview.stopRefresh();
		mListview.stopLoadMore();
		mListview.setRefreshTime("刚刚");
	}

	@Override
	public void onRefresh() {
		page = 1;
		loadingServiceDatas();
	}

	@Override
	public void onLoadMore() {
		loadingServiceDatas();
	}

	@Override
	public void onActivityResult(int reqest, int result, Intent intent) {
		super.onActivityResult(reqest, result, intent);
		if (reqest == REQUEST_REFRESH_CODE) {
			if (intent != null) {
				if (intent != null) {
					NoticeBean bean = (NoticeBean) intent
							.getSerializableExtra("bean");
					if (lastClickposition != -1 && bean != null) {
						myNoticeLists.set(lastClickposition, bean);
						if (mAdapter != null)
							mAdapter.notifyDataSetChanged();

					}
				}
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (myNoticeLists != null && arg2 - 1 >= 0) {
			Intent intent = new Intent(mActivity, NoticeDetailsActivity.class);
			intent.putExtra(NoticeDetailsActivity.NOTICE_MSG_ID,
					myNoticeLists.get(arg2 - 1).id);
			intent.putExtra(NoticeDetailsActivity.NOTICE_MSG_INFOS,
					myNoticeLists.get(arg2 - 1));
			startActivityForResult(intent, REQUEST_REFRESH_CODE);
			lastClickposition = arg2 - 1;
		}

	}

	@Override
	public void onClickMenu(int code, int position) {
		if (code == 1) {// delete
			deleteNotice(position);
		} else if (code == 2) {// copy
			AppUtils.copy(myNoticeLists.get(position).content, mActivity);

			ToastUtil.showToast(mActivity, "已复制到剪贴板");
		} else if (code == 3) {// share
			if (shareDialog == null) {
				shareDialog = new ShareDialog(mActivity,
						myNoticeLists.get(position).title, MContants.SHARE_URL);
			}
			shareDialog.showShareDialog();
		}
	}

	@Override
	public void onClickMenu(View v, int position) {
		deleteNotice(position);
	}

	private void deleteNotice(final int position) {
		showLoading();
		NoticeBean bean = myNoticeLists.get(position);
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("message_id", bean.id);
		sendMap.put("student_id", mSpUtil.getCurrentChildId());
		String requestUrl = ParamsUtils.buildParams(NetWorkApi.DELET_NOTICE,
				sendMap);
		MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {
			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				Log.e("===", result);
				if (resuleCode == RESULTCODE.SUCCESS) {
					myNoticeLists.remove(position);
					if (mAdapter != null)
						mAdapter.notifyDataSetChanged();
					hideLoading();
					Toast.makeText(mActivity, "删除成功", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

	}
}
